using FytSoa.Domain.Erp;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Erp;

/// <summary>
/// erp-采购申请物料表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v9")]
public class ErpPurchaseApplyProductService : IApplicationService 
{
    private readonly SugarRepository<ErpPurchaseApplyProduct> _thisRepository;
    public ErpPurchaseApplyProductService(SugarRepository<ErpPurchaseApplyProduct> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<List<ErpPurchaseApplyProductDto>> GetPagesAsync(WhereParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .WhereIF(param.Id!=0,m=>m.ApplyId==param.Id)
            .OrderByDescending(m => m.Id)
            .ToListAsync();
        if (query.Count==0)
        {
            return query.Adapt<List<ErpPurchaseApplyProductDto>>();
        }
        var productRepository = _thisRepository.ChangeRepository<SugarRepository<ErpProduct>>();
        var productList = await productRepository.AsQueryable()
            .Where(
            m=>query.Select(s=>s.ProductId).Contains(m.Id))
            .Includes(m=>m.Unit)
            .ToListAsync();
        var result=query.Adapt<List<ErpPurchaseApplyProductDto>>();
        foreach (var item in result)
        {
            var product = productList.FirstOrDefault(m => m.Id == item.ProductId);
            if (product==null)
            {
                continue;
            }
            item.Product = product;
        }
        return result;
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<ErpPurchaseApplyProductDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<ErpPurchaseApplyProductDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(ErpPurchaseApplyProductDto model) =>
        await _thisRepository.InsertAsync(model.Adapt<ErpPurchaseApplyProduct>());

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(ErpPurchaseApplyProductDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<ErpPurchaseApplyProduct>());

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
